import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'
import store from "@/store/index"

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    
  },
  
  {
    path:"/songsview/:id",
    name:"SongsView",
    component:()=>import("@/views/SongsView.vue")

  },
  {
    path:"/songsdetail/:id",
    name:"SongsDetail",
    component:()=>import("@/views/SongsDetail.vue")
  },
  {
    path:"/search",
    name:"Search",
    component:()=>import("@/views/SearchView.vue"),
  },
  {
    path:"/my",
    name:"My",
    component:()=>import("@/views/MyHome.vue")  ,
    beforeEnter:(to,from,next)=>{
     const token=sessionStorage.getItem("token")
     if(token){
      store.commit("changeLogin",true)
      next()
     }
    else if(store.state.isLogin==false){
      next("/login")
     }
     else{
      next()
     }
    }
  },
  {
    path:"/login",
    name:"Login",
    component:()=>import("@/views/Login.vue")
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
  router.beforeEach((to,from,next)=>{
   
    
    if(to.name=="Login"){
      store.commit("changeFooterShow",false)
      next()
    }else{
      store.commit("changeFooterShow",true)
      next()
    }
  })
export default router
